
package com.wulian.user.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.wulian.user.dto.TreeTree;
import com.wulian.user.entity.SysTree;
import com.wulian.user.mapper.SysTreeMapper;
import com.wulian.user.service.SysTreeService;
import com.wulian.user.util.TreeUtil;

/**
 * <p>
 * 树 服务实现类
 * </p>
 *
 * @author Administrator
 * @since 2018-01-20
 */
@Service
@Transactional
public class SysTreeServiceImpl extends ServiceImpl<SysTreeMapper,SysTree> implements SysTreeService {

    @Autowired
    private SysTreeMapper mapper;


    @Override
    public List<TreeTree> selectListTree(EntityWrapper<SysTree> sysTreeEntityWrapper) {
        return getDeptTree(this.selectList(sysTreeEntityWrapper), 0);
    }

    @Override
    public List<TreeTree> treeByIdAndType(SysTree sysTree) {
        HashMap<String, Object> map = new HashMap<>();
            map.put( "typeId",sysTree.getTypeId() );
            map.put( "chriledId",sysTree.getChriledId() );
        SysTree sysTreeByChriledId = mapper.getCodeByIdAndType(map);
        HashMap<String, Object> map1 = new HashMap<>();
            map1.put( "typeId",sysTreeByChriledId.getTypeId() );
            map1.put( "code",sysTreeByChriledId.getCode() );
        List<SysTree> sysTrees = mapper.getListByCodeAndType(map1);
        return getDeptTree(sysTrees, Integer.parseInt( sysTreeByChriledId.getParentId().toString() ));
    }


    @Transactional
    @Override
    public Boolean insertSysTree(SysTree sysTree) {
        String code = getCodeBySuperCode(sysTree);
        sysTree.setCode( code );

        Integer insert = mapper.insert( sysTree );
        if(insert>0){
            return true;
        }

        return false;
    }

    @Override
    public List<TreeTree> selectChannelBydeptId(List<Long> longs) {
        List<SysTree> sysTrees = new ArrayList<>();
        for (Long id : longs) {
            HashMap<String, Object> map = new HashMap<>();
            map.put( "typeId",3);
            map.put( "chriledId",id );
            SysTree sysTree = mapper.getCodeByIdAndType(map);
            if(!Objects.isNull(sysTree)){
                sysTrees.add( sysTree );
            }
        }
        return getDeptTree(sysTrees,0);
    }

    @Override
    public List<SysTree> ListTreeByCurrentLandholder(SysTree condition) {

        return null;
    }

    @Override
    public List<SysTree> selectListChannel(SysTree sysTree) {
        HashMap<String, Object> map = new HashMap<>();
        map.put( "typeId",sysTree.getTypeId() );
        map.put( "chriledId",sysTree.getChriledId() );
        SysTree sysTreeByChriledId = mapper.getCodeByIdAndType(map);
        HashMap<String, Object> map1 = new HashMap<>();
        map1.put( "typeId",sysTreeByChriledId.getTypeId() );
        map1.put( "code",sysTreeByChriledId.getCode() );
        List<SysTree> sysTrees = mapper.getListByCodeAndType(map1);
        return sysTrees;
    }

    @Override
    public List<TreeTree> getparentTeamByDeptId(List<Long> longs) {
        List<SysTree> sysTrees = new ArrayList<>();
        for (Long id : longs) {
            HashMap<String, Object> map = new HashMap<>();
            map.put( "typeId",4);
            map.put( "chriledId",id );
            SysTree sysTree = mapper.getCodeByIdAndType(map);
            sysTrees.add( sysTree );
        }
        return getDeptTree(sysTrees,0);
    }

    @Override
    public List<SysTree> getOneTrreByTypeId(Integer typeId) {
        return mapper.getOneTrreByTypeId(typeId);
    }

    @Override
    public List<SysTree> getListByCodeAndType(HashMap<String, Object> map) {
        return mapper.getListByCodeAndType(map);
    }


    /**
     * 构建树
     *
     * @param trees 树
     * @param root  根节点
     * @return
     */
    private List<TreeTree> getDeptTree(List<SysTree> trees, int root) {
        List<TreeTree> treeList = new ArrayList<>();
        TreeTree node;
        if(trees.size()>0){
            for (SysTree tree : trees) {
                if (tree.getParentId().equals(tree.getChriledId())) {
                    continue;
                }
                node = new TreeTree();
                node.setId(Integer.parseInt( tree.getChriledId().toString() ));
                node.setParentId(Integer.parseInt(tree.getParentId().toString()));
                node.setName(tree.getName());
                treeList.add(node);
            }
        }
        return TreeUtil.build(treeList, root);
    }

    public synchronized String getCodeBySuperCode(SysTree sysTree) {
            String code = "";
            HashMap<String, Object> map = new HashMap<>();
            map.put( "typeId",sysTree.getTypeId() );
            map.put( "parentId",sysTree.getParentId() );
            Integer codeCount =  mapper.selectCodeCount(map);
        if(codeCount==0){
            HashMap<String, Object> map1 = new HashMap<>();
            map1.put( "typeId",sysTree.getTypeId() );
            map1.put( "chriledId",sysTree.getParentId() );
            SysTree sysTreeByChriledId = mapper.getCodeByIdAndType(map1);
            code = sysTreeByChriledId.getCode()+"1000";
        }else{
            String maxCode = mapper.selectMaxCode(map);
            BigDecimal decimal = new BigDecimal(maxCode);
            BigDecimal add = decimal.add(new BigDecimal("1"));
            code = add.toString();
        }
        return code;
    }
}
